#include "scene1.h"
#include "HelloWorldScene.h"

#include "stdio.h"
#include "stdlib.h"

#include "../cocosExtension/cocosExtension.h"

USING_NS_CC;

#define RAND(MIN,MAX) ((rand()%((MAX)-(MIN))) + (MIN))


enum 
{
	kTagTileMap = 1,
	kTagSpriteManager = 1,
	kTagAnimation1 = 1,
}; 

CCScene* scene1::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::node();
	
	// 'layer' is an autorelease object
	scene1 *layer = scene1::node();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

scene1::scene1()
{
}
scene1::~scene1()
{
	this->setIsTouchEnabled( false );
}

// on "init" you need to initialize your instance
bool scene1::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	srand( time(NULL) );


	// ask director the window size
	CCSize size = CCDirector::sharedDirector()->getWinSize();



	CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
										"CloseNormal.png",
										"CloseSelected.png",
										this,
										menu_selector(scene1::menuCloseCallback) );
	pCloseItem->setPosition( ccp( CCDirector::sharedDirector()->getWinSize().width - 20, CCDirector::sharedDirector()->getWinSize().height - 20) );
  


	CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem,/*pLeftItem, pRightItem, pJumpItem, pRunItem ,*/ NULL);
	pMenu->setPosition( CCPointZero );
	this->addChild(pMenu, 1);


	// add "HelloWorld" splash screen"
	CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");

	// position the sprite on the center of the screen
	pSprite->setPosition( CCPoint(size.width/2, size.height/2) );

	// add the sprite as a child to this layer
	this->addChild(pSprite, 0);
    
	this->setIsTouchEnabled( true );
    //CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this, 0, false );
	scheduleUpdate();
    

	return true;
}


void scene1::draw()
{
	return;
}

void scene1::update(ccTime dt)
{
	//CCDirector::sharedDirector()->replaceScene( HelloWorld::scene() );
}



bool scene1::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	CCPoint touchLocation = touch->locationInView(touch->view());
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);

	printf( "hehe \n" );
    
	return false;
}
void scene1::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
}
void scene1::ccTouchCancelled(CCTouch *touch, CCEvent* event)
{
}
void scene1::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}




void scene1::menuCloseCallback(CCObject* pSender)
{
	CCTransitionGravityGrid *trans = CCTransitionGravityGrid::transitionWithDuration( 2,  HelloWorld::scene() );
	CCDirector::sharedDirector()->replaceScene( trans );

	//trans->release();

	/*
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
	*/
}
